import {HxPost} from '../service/http';
import {hxData} from './hx-data';

const showNotice = (list, index) => {
    if (list.length > 0) {
        uni.showModal({
            title: '',
            content: list[index].content,
            showCancel: false,
            confirmText: '我知道啦',
            success: () => {
                HxPost('readNotice', {contentId: list[index].contentId});
                index++;
                if (index < list.length) {
                    showNotice(list, index);
                }
            }
        });
    }
};

const showRewardInfo = (unTakeCnt) => {
    if (unTakeCnt > 0 && hxData.get('isFirst')) {
        hxData.set('isFirst', false);
        uni.showModal({
            title: '',
            content: '您有' + unTakeCnt + '个奖励待领取',
            showCancel: false,
            confirmText: '确定',
            success: () => {
            }
        });
    }
};

const sub = () => {
    uni.$on('payNum', (param) => {
        if (Number(param.text) > 0) {
            uni.setTabBarBadge(param);
        } else {
            uni.removeTabBarBadge({index: param.index});
        }
    });

    uni.$on('notice', (list) => {
        showNotice(list, 0);
    });

    uni.$on('rewardNum', (unTakeCnt) => {
        hxData.set('unTakeCnt', unTakeCnt);
        showRewardInfo(unTakeCnt);
    });
};

const pub = () => {
};
export const HxEvent = {
    sub,
    pub,
};
